package com.example.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yang
 * 自定义认证服务器
 */
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

  @Autowired public PasswordEncoder passwordEncoder;

  @Autowired public UserDetailsService mallUserDetailsService;

  @Autowired private AuthenticationManager authenticationManager;

  @Autowired private DataSource dataSource;

  @Autowired
  private JwtTokenStore jwtTokenStore;

  @Autowired
  private JdbcTokenStore jdbcTokenStore;

  @Autowired
  private JwtAccessTokenConverter jwtAccessTokenConverter;

  @Autowired
  private TokenEnhancer jwtTokenEnhancer;

  @Override
  public void configure(final AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    /**
     * jwt 增强模式
     */
    TokenEnhancerChain enhancerChain	= new TokenEnhancerChain();
    List<TokenEnhancer> enhancerList	= new ArrayList<>();
    enhancerList.add( jwtTokenEnhancer );
    enhancerList.add( jwtAccessTokenConverter );
    enhancerChain.setTokenEnhancers( enhancerList );
    endpoints.
            // 配置token存储方式
            tokenStore( jdbcTokenStore )
            // 要使用refresh_token的话，需要额外配置userDetailsService
            .userDetailsService( mallUserDetailsService )
            /**
             * 开启密码授权类型
             * 支持 password 模式
             */
            .authenticationManager( authenticationManager )
            .tokenEnhancer( enhancerChain )
            .accessTokenConverter( jwtAccessTokenConverter );
  }

  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.jdbc(dataSource);
    // JdbcClientDetailsServiceBuilder jcsb = clients.jdbc(dataSource);
    // jcsb.passwordEncoder(passwordEncoder);
  }

  @Override
  public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    security.allowFormAuthenticationForClients();
    security.checkTokenAccess("isAuthenticated()");
    security.tokenKeyAccess("isAuthenticated()");
  }


}
